package com.vastcom.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.vastcom.model.Department;
import com.vastcom.model.Guadian;
import com.vastcom.model.Stuinfo;
import com.vastcom.service.ManageService;
import com.vastcom.vo.Parm;
import com.vastcom.vo.StuinfoVo;



public class StudentAction extends BaseAction {
	
	@Autowired
	private ManageService manageservice;
	
	private Department department;
	
	private Stuinfo stuinfo;
	
	private Guadian guadian;
	
	private Parm parm;
	
	private String page;
	private String rows;
	
	
	
	
	
	
	



	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public Parm getParm() {
		return parm;
	}

	public void setParm(Parm parm) {
		this.parm = parm;
	}

	public Guadian getGuadian() {
		return guadian;
	}

	public void setGuadian(Guadian guadian) {
		this.guadian = guadian;
	}

	public Stuinfo getStuinfo() {
		return stuinfo;
	}

	public void setStuinfo(Stuinfo stuinfo) {
		this.stuinfo = stuinfo;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}
	
	
	
	@Action("addDepartment")
	public String addDepartment() throws Exception{
		
		boolean flag=manageservice.addDepartment(department);
		
		
		json.put("msg", flag);
		
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	//删除院系
	@Action("deleteDepartment")
	public String deleteDepartment() throws Exception{
		
		boolean flag=manageservice.deleteDepartment(department);
		json.put("msg", flag);
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	@Action("getSingleDepartment")
	public String getSingleDepartment() throws Exception{
		
		
		Department temp=manageservice.getDepartment(department);
		
		if(temp!=null){
			json.put("msg", temp);
		}
		else{
			json.put("msg", "empty");
		}
		
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	
	@Action("upateDepartment")
	public String upateDepartment() throws Exception{
		
		boolean flag=manageservice.updateDepartment(department);
		json.put("msg", flag);
		this.setJsonString(json.toString());
		return "json";
	}
	
	@Action("getDepartmentList")
	public String getDepartmentList() throws Exception{
		List<Department> dl=manageservice.getDepartmentList();
			json.put("rows", dl);
		json.put("total", dl.size());
		this.setJsonString(json.toString());
		return "json";
	}
	
	
	@Action("addStuinfo")
	public String addStuinfo() throws Exception{
		
		boolean flag=manageservice.addStuinfo(stuinfo);
		json.put("msg", flag);
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	
	@Action("deleteStuinfo")
	public String deleteStuinfo() throws Exception{
		boolean flag=manageservice.deleteStuinfo(stuinfo);
		json.put("msg", flag);
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	@Action("getSingleStuinfo")
	public String getSingleStuinfo() throws Exception{
		
		Stuinfo temp=manageservice.getStuinfo(stuinfo);
		if(temp!=null)
		json.put("stuinfo", temp);
		else{
			json.put("stuinfo", "error");
		}
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	@Action("updateStuinfo")
	public String updateStuinfo() throws Exception{
		boolean flag=manageservice.updateStuinfo(stuinfo);
		json.put("msg", flag);
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	

	
	
	@Action("getStuinlolist")
		public String getStuinlolist() throws Exception{
		List<Stuinfo> sl=new ArrayList<Stuinfo>();
		int intPage = Integer.parseInt((page == null || page.equals("0") ? "1" : page));
		int num = Integer.parseInt((rows == null || rows.equals("0") ? "10" : rows));
		int start = (intPage - 1) * num;
		    
List<StuinfoVo> vl=new ArrayList<StuinfoVo>();
	 if(parm==null){
			 sl=manageservice.getStuinfoList(start, num);
				json.put("total", manageservice.CountTableSize("Stuinfo"));
				
		}
	 
	 else 		if(parm.getKey().equals("yx")){
		 sl=manageservice.getStuinfoListByDeptid(parm.getValue());
			json.put("total", sl.size());
		
		}
		else if(parm.getKey().equals("bj")){
		 sl=manageservice.getStuinfoListByClassId(parm.getValue());
			json.put("total", sl.size());
		
		}
	 
	 for(Stuinfo s:sl){
		 StuinfoVo v=new StuinfoVo();
		String bjmc=manageservice.getDeptNameById(s.getBj());
		v.setBj(bjmc);
		v.setCsny(s.getCsny()==null?null:s.getCsny()	);
		v.setJg(s.getJg());
		v.setLxdh(s.getLxdh());
		v.setMz(s.getMz());
		v.setRdsj(s.getRdsj()==null?null:s.getRdsj());
		v.setSfzh(s.getSfzh());
		v.setStuinfoid(s.getStuinfoid());
		v.setXb(s.getXb());
		v.setXh(s.getXh());
		v.setXm(s.getXm());
		String yxmc=manageservice.getDeptNameById(s.getYx());
		v.setYx(yxmc);
		v.setZzmm(s.getZzmm());
		
		vl.add(v);
		
		 
		 
		 
	 }
		
			
				
				
	 json.put("rows", vl);
					
					setJsonString(json.toString());
					return "json";
		}
	
	
	
	@Action("addGuadian")
	public String addGuadian() throws Exception{
		boolean flag=manageservice.addGuadian(guadian);
		json.put("msg", flag);
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	
	@Action("deleteGuadian")
	public String deleteGuadian() throws Exception{
		boolean flag=manageservice.deleteGuadian(guadian);
		json.put("msg", flag);
		this.setJsonString(json.toString());
		return "json";
	}
	
	@Action("getSingleGuadian")
	public String getSingleGuadian() throws Exception{
		
		Guadian temp=manageservice.getGuadian(guadian);
		if(temp!=null){
			json.put("msg", temp);
		}
		else{
			json.put("msg", "empty");
		}
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	
	@Action("getGuadianList")
	public String getGuadianList() throws Exception{
		
		List<Guadian> gl=manageservice.getGuadianList();
		
		json.put("total", gl.size());
		json.put("rows", gl);
		
		
		this.setJsonString(json.toString());
		return "json";
	}
	
	

}
